﻿@using System.Globalization
@model  Model.DictionaryInfo
@{
    ViewBag.Title = "编辑模型标签";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@section Head{}
<script type="text/javascript">
    var UrlArray = $.getUrlArray();
    var retData = new Array();
    var opts = {
        height: 380,
        rows: 15,
        idField: "GUID",
        para: { TypeID: UrlArray["id"], dbName: encodeURI(UrlArray["dbName"]) },
        checkbox: true,
        columns: [
                    { field: 'DefaultView', title: '模型名称', width: 40, sortable: true },
                    { field: 'TableDescription', title: '描述', width: 40, sortable: true },
                    { field: 'CreateTime', title: '创建时间', width: 10, align: 'center', sortable: true },
                    { field: 'GUID', title: '操作', width: 10, align: 'center', formatter: function (value, rowData, rowIndex) {
                        var add = true;
                        for (var j = 0; j < retData.length; j++) {
                            if (retData[j].GUID == value) {
                                add = false;
                            }
                        }
                        if (add) {
                            retData.push(rowData);
                        }
                        return "<a href='#' onclick='DeleteItem(\"" + value + "\")'>删除</a>";
                    }
                    }
            ]
    };
    $(function () {
        if (UrlArray["id"]) {
            opts.url = "/ModelConfig/GetList";
        }
        $("#DataGrid").dg(opts);
    })
    var arr_del = new Array();
    function DelMore() {
        $("#DataGrid").find(":checkbox").each(function (i, d) {
            if (d.value != "on" && d.checked) {
                arr_del.push(d.value);
                for (var j = 0; j < retData.length; j++) {
                    if (retData[j].GUID == d.value) {
                        retData.removeAt(j);
                    }
                }
            }
        });
        if (arr_del.length == 0) {
            alert("请选择模型。");
            return;
        }
        if (confirm("确定要删除吗？")) {
            $.post("/ModelConfig/Delete", { Type: 3, ids: arr_del.join(","), dbName: encodeURI(UrlArray["dbName"]) }, function (result) {
                if (result == "1") {
                    alert("操作成功。");
                }
                else
                    $("#msg").html(result);
                $("#DataGrid").dg(opts);
            });
        } else
            $("#DataGrid").dg(opts);
    }
    function DeleteItem(v) {
        if (confirm("确定要删除吗？")) {
            for (var j = 0; j < retData.length; j++) {
                if (retData[j].GUID == v) {
                    retData.removeAt(j);
                }
            }
            $.post("/ModelConfig/Delete", { Type: 3, id: v, dbName: encodeURI(UrlArray["dbName"]) }, function (result) {
                if (result == "1") {
                    alert("操作成功。");
                }
                else
                    $("#msg").html(result);
                $("#DataGrid").dg(opts);
            });
        } else
            $("#DataGrid").dg(opts);
    }
    function save() {
        if ($.trim($("#txt_Name").val()) == "") {
            alert("标签名称不能为空。");
            return;
        }
        var list = new Array();
        for (var i = 0; i < retData.length; i++) {
            list.push(retData[i].GUID);
        }
        var para = {
            id: "@Model.Guid",
            Name: $("#txt_Name").val(),
            Description: $("#txt_Description").val(),
            ModelList: list.join(","),
            dbName: encodeURI(UrlArray["dbName"])
        };
        $.post("/ModelConfig/SaveTypeData", para, function (result) {
            if (result == "1") {
                alert("操作成功！");
                window.location = "/ModelConfig/Index?dbName=" + encodeURI(UrlArray["dbName"]);
            }
            else
                $("#msg").html(result);
        });
    }
    function back() {
        window.location = "/ModelConfig/Index?dbName=" + encodeURI(UrlArray["dbName"]);
    }
    function AddItem() {
        showModalMany('Contact', '/ModelConfig/ModelList?dbName=' + encodeURI(UrlArray["dbName"]), '900', '480', returnFun);
    }
    function returnFun(v) {
        var addModelIDs = new Array();
        for (var i = 0; i < v.length; i++) {
            var add = true;
            for (var j = 0; j < retData.length; j++) {
                if (retData[j].GUID == v[i].GUID) {
                    add = false;
                }
            }
            if (add) {
                retData.push(v[i]);
                addModelIDs.push(v[i].GUID);
            }
        }
        if (UrlArray["id"]) {
            $.post("/ModelConfig/AddModelToType", { id: UrlArray["id"], ids: addModelIDs.join(","), dbName: encodeURI(UrlArray["dbName"]) }, function (result) {
                if (result != "1")
                    $("#msg").html(result);
            });
        }
        var data = { "total": v.length, "rows": retData };
        opts["data"] = data;
        $("#DataGrid").dg(opts);
    }
     
</script>
<div style="margin: 5px;">
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/edit.png" />
            </td>
            <td width="300">
                编辑模型标签
            </td>
            <td align="right">
                <input type="button" value="保存" onclick="save();" class="button" />
                <input type="button" value="返回" onclick="back();" class="button" />
            </td>
        </tr>
    </table>
    <table class="searchArea" width="100%" height="500">
        <tr>
            <td valign="top">
                标签名称：<input type="text" id="txt_Name" value="@Model.Name" maxlength="50" style="width: 450px;" /><span
                    class="red">*</span><br />
                标签描述：<textarea id="txt_Description" style="width: 450px; height: 50px;">@Model.Description</textarea>
                <input type="hidden" id="txt_GUID" value="@Model.Guid" /><br />
                <table class="listTitle">
                    <tr>
                        <td width="15">
                            <img src="/images/edit.png" />
                        </td>
                        <td width="300">
                            导入模型
                        </td>
                        <td align="right">
                            <input type="button" value="导入模型" onclick="AddItem();" class="button" />
                            <input type="button" value="删除模型" onclick="DelMore();" class="button" />
                        </td>
                    </tr>
                </table>
                <table id="DataGrid" class="pagTable" cellpadding="0" cellspacing="0">
                </table>
                <div>
                    <div id="Pagination" class="pagination">
                    </div>
                    <div id="loading" class="loading">
                        <img src="/Images/tree_loading.gif" />
                    </div>
                </div>
                <div id="msg">
                </div>
            </td>
        </tr>
    </table>
</div>
